/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unb.core.status;

import br.unb.core.message.Message;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 *
 * @author spider
 */
public class StatusSocket extends br.unb.core.message.Socket {

    private static final StatusSocket INSTANCE = new StatusSocket();//no lazy initialization

    public static StatusSocket getInstance() {
        return INSTANCE;
    }

    private StatusSocket() {
        try {
            super.setName("7777");
            socket = new DatagramSocket(STATUS_PORT);
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public void sendStatus(String message, String dst) {
        try {
            message = "FROM "
                    + InetAddress.getLocalHost().toString()
                    + ":"
                    + message;
            InetAddress a = InetAddress.getByName(dst);
            //sendStatus(message, a);
            super.send(new Message(Message.Type.DIALOG, message), a, STATUS_PORT);
        } catch (UnknownHostException ex) {
        }

    }

    /*public void sendStatus(String message, InetAddress dst) {
    //
    }*/
    /**
     * Waits for the answer to return
     * @param message Mesage to be sent
     * @param dst destiny IP
     */
    public Message wait4Status() {
        return super.receive();
    }
}
